package models.my;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import models.user.UserInfo;

import play.Logger;
import play.db.ebean.Model;
import tyrex.services.UUID;
import utils.MengException;
//16
/**
 * 我的收藏
 * @author zhangpeng
 *
 */

@Entity
@Table(name="my_favorite")
public class MyFavorite extends Model{

	private static final long serialVersionUID = 1L;
	
	@Id
	public long id;
	public String code = UUID.create();
	public String username;
	public String favorite;
	
	public static MyFavorite getInstances(){
		return new MyFavorite();
	}
	
	public static Model.Finder<Long, MyFavorite> find = new Model.Finder<Long, MyFavorite>(Long.class, MyFavorite.class);
	
	/**
	 * 创建用户收藏
	 * 
	 * @param favorite
	 * @return
	 * @throws MengException
	 */
	public boolean createFavorite(MyFavorite favorite) throws MengException {

		if (UserInfo.getInstances().verifyUsername(favorite.username)) {
			throw new MengException("160001", "用户名不存在");
		}

		if (verifyMyFavorite(favorite.code)) {
			throw new MengException("160002", "用户收藏已存在");
		}

		try {
			favorite.save();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createFavorite Exception," + t.getMessage());
			throw new MengException("160003", "用户收藏保存失败");
		}
	}
	
	/**
	 * 用户收藏修改
	 * @param favorite
	 * @return
	 * @throws MengException
	 */
	public boolean modifyFavorite(MyFavorite favorite) throws MengException {
		
		if (UserInfo.getInstances().verifyUsername(favorite.username)) {
			throw new MengException("160004", "用户名不存在");
		}

		favorite.id = getFavorite(favorite.code).id;
		try {
			favorite.update();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo modifyFavorite Exception," + t.getMessage());
			throw new MengException("160005", "用户收藏修改失败");
		}
	}
	
	/**
	 * 用户收藏删除
	 * @param code
	 * @return
	 * @throws MengException
	 */
	public boolean destroyFavorite(String code) throws MengException {
		
		try {
			getFavorite(code).delete();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo destroyFavorite Exception," + t.getMessage());
			throw new MengException("160006", "用户收藏删除失败");
		}
	}
	
	/**
	 * 获取用户收藏列表
	 * @param username
	 * @param page
	 * @param size
	 * @return
	 * @throws MengException
	 */
	public List<MyFavorite> getFavoritePage(String username, int page, int size) throws MengException {
		
		try {
			return find.where().eq("username", username).findPagingList(size).getPage(page).getList();
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo getFavoritePage Exception," + t.getMessage());
			throw new MengException("160007", "获取用户收藏列表失败");
		}
	}
	
	/**
	 * 获取用户收藏
	 * @param code
	 * @return
	 * @throws MengException
	 */
	public MyFavorite getFavorite(String code) throws MengException {
		
		try {
			return find.where().eq("code", code).findUnique();
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo getFavorite Exception," + t.getMessage());
			throw new MengException("160008", "获取用户收藏失败");
		}
	}
	
	public boolean verifyMyFavorite(String code) throws MengException {
		MyFavorite favorite = find.where().eq("code", code).findUnique();
		if(favorite == null) {
			return true;
		}
		return false;
	}

}
